package com.example.managemyself.util;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.example.managemyself.MainActivity;
import com.example.managemyself.R;

/**
 * 通知帮助类，用于管理应用通知
 */
public class NotificationHelper {
    
    public static final String CHANNEL_ID_TASKS = "channel_tasks";
    public static final String CHANNEL_ID_POMODORO = "channel_pomodoro";
    public static final String CHANNEL_ID_DIARY = "channel_diary";
    public static final int NOTIFICATION_ID_TASK_REMINDER = 1001;
    public static final int NOTIFICATION_ID_POMODORO = 1002;
    
    /**
     * 创建通知渠道
     */
    public static void createNotificationChannels(Context context) {
        // 只在Android O及更高版本需要创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 任务通知渠道
            NotificationChannel tasksChannel = new NotificationChannel(
                    CHANNEL_ID_TASKS,
                    "任务提醒",
                    NotificationManager.IMPORTANCE_DEFAULT);
            tasksChannel.setDescription("任务截止日期和重要提醒");
            
            // 番茄钟通知渠道
            NotificationChannel pomodoroChannel = new NotificationChannel(
                    CHANNEL_ID_POMODORO,
                    "番茄钟",
                    NotificationManager.IMPORTANCE_HIGH);
            pomodoroChannel.setDescription("番茄钟工作和休息提醒");
            
            // 日记通知渠道
            NotificationChannel diaryChannel = new NotificationChannel(
                    CHANNEL_ID_DIARY,
                    "日记提醒",
                    NotificationManager.IMPORTANCE_LOW);
            diaryChannel.setDescription("日记写作提醒");
            
            // 注册通知渠道
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(tasksChannel);
                notificationManager.createNotificationChannel(pomodoroChannel);
                notificationManager.createNotificationChannel(diaryChannel);
            }
        }
    }
    
    /**
     * 显示任务提醒通知
     */
    public static void showTaskReminder(Context context, String title, String content) {
        if (!SettingsManager.getInstance().isNotificationsEnabled(context)) {
            return;
        }
        
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID_TASKS)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        try {
            notificationManager.notify(NOTIFICATION_ID_TASK_REMINDER, builder.build());
        } catch (SecurityException e) {
            // 处理权限问题
            e.printStackTrace();
        }
    }
    
    /**
     * 显示番茄钟通知
     */
    public static void showPomodoroNotification(Context context, String title, String content) {
        if (!SettingsManager.getInstance().isNotificationsEnabled(context)) {
            return;
        }
        
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID_POMODORO)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        try {
            notificationManager.notify(NOTIFICATION_ID_POMODORO, builder.build());
        } catch (SecurityException e) {
            // 处理权限问题
            e.printStackTrace();
        }
    }
    
    /**
     * 取消通知
     */
    public static void cancelNotification(Context context, int notificationId) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.cancel(notificationId);
    }
}
